/**
 * Helper class for managing packet data
 * @author - bigredshoes03
 * @date - 10/1/2009
 */
package networkcom;

import java.util.Date;

public class Packet {
	private int packet;
	private byte[] data;
	private long timestamp;

	/**
	 * Construct the packet
	 * @param p - server-style opcode
	 * @param data - byte array of data sent by client
	 */
	public Packet(int p, byte[] data){
		packet = p;
		this.data = data;
		timestamp = new Date().getTime();
	}

	/**
	 * Get server style opcode that was sent with
	 * packet
	 * @return - server-style opcode
	 */
	public int getOpcode(){
		return packet;
	}

	/**
	 * Get data as a byte array that was sent
	 * with packet
	 * @return - byte array sent by client
	 */
	public byte[] getRawData(){
		return data;
	}

	public long getTime(){
		return timestamp;
	}
	
	/**
	 * Get data as a string that was sent with
	 * packet (Helper for getting string-type data)
	 * @return - data as a string
	 */
	public String getStringData(){
		String retval = "";
		if(data!=null){
			//Copy byte chars into a string
			for(byte b : data){
				retval += (char)b;
			}
		}
		return retval;
	}

}
